package com.baitai.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baitai.common.utils.SecurityUtils;
import com.baitai.system.domain.vo.PrizeProductInfo;
import com.baitai.system.domain.vo.ProductInfoVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baitai.common.annotation.Log;
import com.baitai.common.core.controller.BaseController;
import com.baitai.common.core.domain.AjaxResult;
import com.baitai.common.enums.BusinessType;
import com.baitai.system.domain.ProductInfo;
import com.baitai.system.service.IProductInfoService;
import com.baitai.common.utils.poi.ExcelUtil;
import com.baitai.common.core.page.TableDataInfo;

/**
 * 商品详情Controller
 * 
 * @author xx
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/system/productInfo")
public class ProductInfoController extends BaseController
{
    @Autowired
    private IProductInfoService productInfoService;

    /**
     * 查询商品详情列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductInfo productInfo)
    {
        startPage();
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品详情列表
     */
    @Log(title = "商品详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductInfo productInfo)
    {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        util.exportExcel(response, list, "商品详情数据");
    }

    /**
     * 获取商品详情详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productInfoService.selectProductInfoById(id));
    }

    /**
     * 新增商品详情
     */
    @Log(title = "商品详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.insertProductInfo(productInfo));
    }

    /**
     * 修改商品详情
     */
    @Log(title = "商品详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.updateProductInfo(productInfo));
    }

    /**
     * 删除商品详情
     */
    @Log(title = "商品详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productInfoService.deleteProductInfoByIds(ids));
    }

    /**
     * 查询商品详情列表
     */
    @PostMapping("/appList")
    public AjaxResult appList(@RequestBody ProductInfo productInfo)
    {
        startPage();
        List<ProductInfo> list = productInfoService.appList(productInfo);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 查询商品详情列表
     */
    @PostMapping("/conversionProduct")
    @Log(title = "商品兑换", businessType = BusinessType.OTHER)
    public AjaxResult conversionProduct(@RequestBody ProductInfoVo productInfo)
    {
        String a = productInfoService.conversionProduct(SecurityUtils.getUserId(),productInfo);
        if(a.equals("兑换成功")){
            return AjaxResult.success(a);
        }else {
            return AjaxResult.error(a);
        }

    }
    /**
     * 查询商品详情列表
     */
    @PostMapping("/purchaseProduct")
    @Log(title = "商品购买", businessType = BusinessType.OTHER)
    public AjaxResult purchaseProduct(@RequestBody ProductInfoVo productInfo)
    {
        AjaxResult result = productInfoService.purchaseProduct(SecurityUtils.getUserId(),productInfo);
        return result;

    }

    /**
     * 查询商品详情列表
     */
    @GetMapping("/prizeDrawProductList/{id}")
    public AjaxResult prizeDrawProductList(@PathVariable("id") Long id)
    {
        List<PrizeProductInfo> list = productInfoService.prizeDrawProductList(id);
        return AjaxResult.success(list);
    }


}
